package com.egame.manage.biz.server.gc;

import java.util.List;

import org.apache.log4j.Logger;

import cn.egame.common.data.BaseDao;
import cn.egame.common.data.SqlUtils;
import cn.egame.common.exception.ExceptionCommonBase;
import cn.egame.common.util.Utils;

import com.egame.manage.biz.domain.TerminalBean;
import com.egame.manage.biz.domain.TerminalGroupInfoBean;
import com.egame.manage.biz.utils.ConstVar;

public class TerminalGroupServiceDao extends BaseDao {
	private static Logger logger = Logger.getLogger(TerminalGroupServiceDao.class);
	
	public TerminalGroupServiceDao() throws ExceptionCommonBase {
		super("egame_game");
	}

	public List<Integer> listTerminalGroupIds(String name,int page,int rowsOfPage) throws ExceptionCommonBase{
		String sql = "select id from t_parameter_app where enable = 1 and parent_id = " + ConstVar.APP_TERMINAL_GROUP;
		if(!Utils.stringIsNullOrEmpty(name)){
				sql += " and name like " + SqlUtils.QuataLikeStr(name);
		}
		sql += " order by update_time desc ";
		sql += " limit " + page * rowsOfPage + "," + rowsOfPage;
		logger.info(sql);
		return this.getListInteger(sql);
	}

	public int getTerminalGroupCount(String name) throws ExceptionCommonBase{
		String sql = "select count(*) from t_parameter_app where enable = 1 and parent_id = " + ConstVar.APP_TERMINAL_GROUP;
		if(!Utils.stringIsNullOrEmpty(name)){
			sql += " and name like " + SqlUtils.QuataLikeStr(name);
		}
		return this.getInt(sql);
	}

	public void updateTerminalGroupIdInTerminal(String[] terminalIds, int groupId) throws ExceptionCommonBase{
		String sql = "update t_terminal set terminal_group_id = "+groupId;
		sql += " where terminal_id in ( "+Utils.toString(terminalIds)+" ) ";
		this.executeUpdate(sql);
	}

	public void updateTerminalGroupIdInTerminalGameFileLink(
			String[] terminalIds, int groupId) throws ExceptionCommonBase{
		String sql = "update t_terminal_game_file_link set terminal_group_id = "+groupId;
		sql += " where terminal_id in ( "+Utils.toString(terminalIds)+" ) ";
		this.executeUpdate(sql);
	}

	public int getTerminalCountByGroupId(int groupId)throws ExceptionCommonBase{
		String sql = "select count(*) from t_terminal where terminal_group_id = "+groupId;
		return this.getInt(sql);
	}

	public void clearTerminalGroupIdInTerminal(int groupId)throws ExceptionCommonBase{
		String sql = "update t_terminal set terminal_group_id = 0 ";
		sql += " where terminal_group_id = "+groupId;
		this.executeUpdate(sql);
	}

	public void clearTerminalGroupIdInTerminalGameFileLink(int groupId)throws ExceptionCommonBase{
		String sql = "update t_terminal_game_file_link set terminal_group_id = 0 ";
		sql += " where terminal_group_id = "+groupId;
		this.executeUpdate(sql);
	}

	public int checkTerminalGroupNameExist(int id, String name) throws ExceptionCommonBase{
		String sql = "select count(*) from t_parameter_app where enable = 1 and parent_id = " + ConstVar.APP_TERMINAL_GROUP;
		sql += " and name = " + SqlUtils.QuataStr(name);
		if(id != 0){
			sql += " and id != " + id;
		}
		return this.getInt(sql);
	}

	public void deleteTerminalGroup(int groupId) throws ExceptionCommonBase{
		if(groupId != 0){
			String sql = "delete from t_parameter_app where id = " + groupId + " and parent_id = " + ConstVar.APP_TERMINAL_GROUP;
			this.executeUpdate(sql);
		}
	}

}
